package com.ibeeking.found.upms.s.rest.service.impl;

import com.ibeeking.found.common.base.BaseDO;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.upms.s.rest.service.IShopRoleAuthService;
import com.ibeeking.found.upms.s.rest.service.IShopRoleService;
import com.ibeeking.found.upms.service.common.bo.ShopRoleTenantAuthRoleBO;
import com.ibeeking.found.upms.service.common.dos.ShopRoleTenantAuthRoleDO;
import com.ibeeking.found.upms.service.common.dos.TenantAuthRoleDO;
import com.ibeeking.found.upms.service.common.dos.TenantAuthRoleUserDO;
import com.ibeeking.found.upms.service.common.param.AuthRoleShopRoleParam;
import com.ibeeking.found.upms.service.common.param.ShopRoleAuthRoleParam;
import com.ibeeking.found.upms.service.common.vo.TenantAuthRoleVO;
import com.ibeeking.found.upms.service.mapper.ShopRoleTenantAuthRoleMapper;
import com.ibeeking.found.upms.service.mapper.TenantAuthRoleMapper;
import com.ibeeking.found.upms.service.mapper.TenantAuthRoleUserMapper;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ShopRoleAuthServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-04-27 13:42
 **/
@Service
public class ShopRoleAuthServiceImpl implements IShopRoleAuthService {

    @Resource
    private TenantAuthRoleMapper tenantAuthRoleMapper;
    @Resource
    private ShopRoleTenantAuthRoleMapper shopRoleTenantAuthRoleMapper;
    @Resource
    private TenantAuthRoleUserMapper tenantAuthRoleUserMapper;
    @Resource
    private IShopRoleService roleServiceImpl;

    @Override
    public List<TenantAuthRoleVO> effectiveAuthList() {
        List<TenantAuthRoleDO> tenantAuthRoleDos = tenantAuthRoleMapper.selectList(new LambdaUpdateWrapper<>(TenantAuthRoleDO.class)
                .orderByAsc(TenantAuthRoleDO::getSort)
                .orderByDesc(BaseDO::getCreateTime)
                .eq(TenantAuthRoleDO::getPublishStatus, PublishStatusEnum.ENABLE.getValue())
        );
        return JsonUtils.jsonToList(tenantAuthRoleDos, TenantAuthRoleVO.class);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean authRoleByShopRoleId(Long shopId, AuthRoleShopRoleParam param) {
        //商户角色
        Long shopRoleId = param.getShopRoleId();
        //租户授权角色
        List<Long> authRoleIds = param.getAuthRoleIds();
        //清空现关联的角色
        shopRoleTenantAuthRoleMapper.delete(new LambdaUpdateWrapper<>(ShopRoleTenantAuthRoleDO.class)
                .eq(ShopRoleTenantAuthRoleDO::getShopId, shopId)
                .eq(ShopRoleTenantAuthRoleDO::getShopRoleId, shopRoleId)
        );
        if (Collections3.isEmpty(authRoleIds)) {
            return true;
        }
        //新增关系
        List<ShopRoleTenantAuthRoleDO> saves = new ArrayList<>();
        authRoleIds.stream().forEach(key -> {
            ShopRoleTenantAuthRoleDO userRoleDO = new ShopRoleTenantAuthRoleDO();
            userRoleDO.setShopId(shopId);
            userRoleDO.setShopRoleId(shopRoleId);
            userRoleDO.setTenantAuthId(key);
            saves.add(userRoleDO);
        });
        // 新增关系
        shopRoleTenantAuthRoleMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean shopRoleByAuthRoleId(Long shopId, ShopRoleAuthRoleParam param) {
        //租户授权角色
        Long authRoleId = param.getAuthRoleId();
        //商户角色
        List<Long> shopRoleIds = param.getShopRoleIds();
        //清空现关联的角色
        shopRoleTenantAuthRoleMapper.delete(new LambdaUpdateWrapper<>(ShopRoleTenantAuthRoleDO.class)
                .eq(ShopRoleTenantAuthRoleDO::getShopId, shopId)
                .eq(ShopRoleTenantAuthRoleDO::getTenantAuthId, authRoleId)
        );
        if (Collections3.isEmpty(shopRoleIds)) {
            return true;
        }
        //查询管理员
        Long administratorId = roleServiceImpl.selectAdministratorId();
        if (shopRoleIds.contains(administratorId)) {
            shopRoleIds.clear();
            shopRoleIds.add(administratorId);
        }
        //新增关系
        List<ShopRoleTenantAuthRoleDO> saves = new ArrayList<>();
        shopRoleIds.stream().forEach(key -> {
            ShopRoleTenantAuthRoleDO userRoleDO = new ShopRoleTenantAuthRoleDO();
            userRoleDO.setShopId(shopId);
            userRoleDO.setShopRoleId(key);
            userRoleDO.setTenantAuthId(authRoleId);
            saves.add(userRoleDO);
        });
        // 新增关系
        shopRoleTenantAuthRoleMapper.insertBatchSomeColumn(saves);
        return true;
    }

    @Override
    public List<ShopRoleTenantAuthRoleBO> queryByShopRoleId(Long shopId, Long shopRoleId) {
        List<ShopRoleTenantAuthRoleDO> shopRoleTenantAuthRoleDos = shopRoleTenantAuthRoleMapper.selectList(new LambdaQueryWrapper<>(ShopRoleTenantAuthRoleDO.class)
                .eq(ShopRoleTenantAuthRoleDO::getShopId, shopId)
                .eq(ShopRoleTenantAuthRoleDO::getShopRoleId, shopRoleId)
        );
        return JsonUtils.jsonToList(shopRoleTenantAuthRoleDos, ShopRoleTenantAuthRoleBO.class);
    }

    @Override
    public List<ShopRoleTenantAuthRoleBO> queryByAuthRoleId(Long shopId, Long authRoleId) {
        List<ShopRoleTenantAuthRoleDO> shopRoleTenantAuthRoleDos = shopRoleTenantAuthRoleMapper.selectList(new LambdaQueryWrapper<>(ShopRoleTenantAuthRoleDO.class)
                .eq(ShopRoleTenantAuthRoleDO::getShopId, shopId)
                .eq(ShopRoleTenantAuthRoleDO::getTenantAuthId, authRoleId)
        );
        return JsonUtils.jsonToList(shopRoleTenantAuthRoleDos, ShopRoleTenantAuthRoleBO.class);
    }

    @Override
    public List<ShopRoleTenantAuthRoleBO> queryAuthShopIds(Long shopId, Long tenantUserId) {
        //查询当前租户被租户授权的角色
        List<TenantAuthRoleUserDO> tenantAuthRoleUserDos = tenantAuthRoleUserMapper.selectList(new LambdaQueryWrapper<>(TenantAuthRoleUserDO.class)
                .eq(TenantAuthRoleUserDO::getTenantUserId, tenantUserId)
        );
        if (Collections3.isEmpty(tenantAuthRoleUserDos)) {
            return Collections3.emptyList();
        }
        //根据授权角色id查询关联的商户id列表
        List<ShopRoleTenantAuthRoleDO> shopRoleTenantAuthRoleDos = shopRoleTenantAuthRoleMapper.selectList(new LambdaQueryWrapper<>(ShopRoleTenantAuthRoleDO.class)
                .eq(null != shopId, ShopRoleTenantAuthRoleDO::getShopId, shopId)
                .in(ShopRoleTenantAuthRoleDO::getTenantAuthId, tenantAuthRoleUserDos.parallelStream().map(TenantAuthRoleUserDO::getTenantAuthRoleId).collect(Collectors.toList()))
        );
        return JsonUtils.jsonToList(shopRoleTenantAuthRoleDos, ShopRoleTenantAuthRoleBO.class);
    }
}
